package com.mfs.common.utills;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * 简单加密工具包
 * @author mfs
 * @date 2022/08/30
 * */
public class EncryptUtil {
    /**
     * md5获取信息摘要
     * */
    public static byte[] getMessageDigestByMd5(byte [] bytes) {
        if (bytes == null || bytes.length < 1) {
            return new byte[0];
        }
        try {
            MessageDigest md5 = MessageDigest.getInstance("MD5");
            return md5.digest(bytes);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return new byte[0];
    }

    /**
     * md5获取信息摘要
     * */
    public static String getMessageDigestByMd5(String msg) {
        if (msg == null) {
            return null;
        }
        try {
            StringBuffer sb = new StringBuffer();
            byte[] digest = getMessageDigestByMd5(msg.getBytes("utf8"));
            for (byte b : digest) {
                String s = Integer.toHexString(b & 0xff);
                if (s.length() < 2) {
                    sb.append('0');
                }
                sb.append(s);
            }
            return sb.toString();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
}
